import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import {AppService} from "../../../services/app.service";
import {FormBuilder, FormControl, FormGroup, Validators} from "@angular/forms";
import {CustomValidators} from "ng2-validation";
import {Md5} from "ts-md5";
import {ResDataModel} from "../../../models/res-data-model";

/**
 * Generated class for the ModifyPasswordPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-modify-password',
  templateUrl: 'modify-password.html',
})
export class ModifyPasswordPage {

  public myForm:FormGroup;//form表单中的值

  validation_messages = {
    'initPass':'密码不能为空，且至少为六位数',
    'surePass':'两次密码输入不一致',
  };

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              public formBuilder:FormBuilder,
              private appService:AppService) {

    let initPass = new FormControl('', [Validators.required,Validators.minLength(6)]);
    this.myForm=this.formBuilder.group({
      initPass,
      surePass:["",CustomValidators.equalTo(initPass)],
    });
  }

  ionViewDidLoad() {
  }

  /**
   * 修改密码
   */
  modifyPassword(){
    event.preventDefault();
    let formModel = this.myForm.value;
    let postData = {
      password:Md5.hashStr(formModel.initPass)
    }
    this.appService.httpPost('AppUserCtrl_modifyMyPassword',postData,null,(res:ResDataModel)=>{
      console.log(res.datas);
      if(res.datas[0].value == "success"){
        //注册成功跳转登陆页
        this.appService.smallToast('密码修改成功','success');
        this.navCtrl.popToRoot();
      }
    },true);
  }
}
